home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / conhlp03 / fswchsrc / sel_menu.c < prev    next >
Text File  |  1994-11-16  |  2KB  |  90 lines

  1. /*
  2.     sel_menu.c
  3.     カーソルによる選択メニュー 汎用
  4.     1994/02/27
  5. */
  6.  
  7. #include <stdio.h>
  8.  
  9. #ifdef TOWNS
  10. unsigned char getop( void );            /* パッド & キーBIOS */
  11. #else
  12. #define getop getch
  13. #endif
  14.  
  15. #define LEFT (29)
  16. #define RIGHT (28)
  17. #define UP (30)
  18. #define DOWN (31)
  19. #define EXEC (18)
  20. #define QUIT (17)
  21. #define ESCKEY (0x1b)
  22.  
  23. #define ESC "\x1b["
  24. #define NOM "0m"
  25. #define REV "7m"
  26.  
  27. #define ON 1
  28. #define OFF 0
  29.  
  30. char us[30];
  31.  
  32. void locdisp(int x,int w,int firstline,char a,char *s){
  33.     if( a=='R' ) cputs(ESC REV);
  34.     else
  35.     if( a=='N' ) cputs(ESC NOM);
  36.     else return;
  37.     cprintf(us,firstline+(x/w),(x%w)*(80/w),s);
  38. }
  39.  
  40. int sel_menu( int w, int firstline, char *word[] ){/* 選択メニュー */
  41.     /* w: 横並びの数 */
  42.     /* firstline: 表示開始行 */
  43.     /* word: 選択項目のポインタ */
  44.     int m,i,b;
  45.     char endf=OFF; /* メニュー終了フラグ */
  46.  
  47.     /* 出力書式の設定 */
  48.     sprintf(us,ESC"%%d;%%df[%%-%d.%ds]",80/w-5,80/w-5);
  49.  
  50.     /* firstline 以降の消去 */
  51.     cprintf(ESC"%d;0f"ESC"0J",firstline);
  52.  
  53.     /* 選択肢の表示 */
  54.     for(m=0;word[m][0]!=NULL;m++)
  55.         locdisp(m,w,firstline,'N',word[m]); /* m : 選択肢の数 */
  56.  
  57.     i=b=0;
  58.     /* カーソル選択 */
  59.     while(endf==OFF){
  60.         locdisp(b,w,firstline,'N',word[b]);
  61.         locdisp(i,w,firstline,'R',word[i]);
  62.         b = i;
  63.         switch (getop()) {
  64.         case LEFT: /* カーソル ← */
  65.             i = (i+m-1) % m;
  66.             break;
  67.         case RIGHT: /* カーソル → */
  68.             i = (i+1) % m;
  69.             break;
  70.         case UP: /* カーソル ↑ */
  71.             i = (i+m-w) % m;
  72.             break;
  73.         case DOWN: /* カーソル ↓ */
  74.             i = (i+w) % m;
  75.             break;
  76.         case EXEC: /* 実行 key */
  77.         case 0x0d: /* RETkey */
  78.             endf=ON;
  79.             break;
  80.         case QUIT: /* 取消 key */
  81.         case ESCKEY: /* ESC key */
  82.             cputs(ESC"37;0m");
  83.             return -1; /* 取り止め */
  84.         default: ;
  85.         } /* switch */
  86.     } /* while(endf==OFF) */
  87.     locdisp(i,w,firstline,'N',word[i]);
  88.     return i;
  89. }
  90.